package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.mapper.StudentMapper;
import com.example.demo.mapper.TeacherMapper;
import com.example.demo.mapper.WarningInfoMapper;
import com.example.demo.entity.*;
import com.example.demo.util.WarnNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/show")
public class ViewController {
    @Autowired
    private  StudentMapper sm;
    @Autowired
    private  StudentMapper stum;
    @Autowired
    private WarningInfoMapper wim;
    @Autowired
    private  TeacherMapper tm;
    @RequestMapping("/major")
    public void showMajor(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String college = "";
        User user = (User)request.getSession().getAttribute("user");
        if(user.getRole()==1){
            college = request.getParameter("college");
        }else college=tm.findTeaByName(user.getName()).getCollege();
        String grade = request.getParameter("grade");
        String term = request.getParameter("term");
        if(grade!=null&&!"".equals(grade)&&term!=null&&!"".equals(term)) {
            Map<String, Integer> map = new HashMap<>();
            Set<Integer> course = new HashSet<>();
            List<Student> stuByCollegeAndGrade = stum.findStuByCollegeAndGrade(college, grade);
            for (Student s : stuByCollegeAndGrade) {
                course.add(s.getId());
            }
            List<WarningInfos> warningInfos = wim.selectbyTerm(term);
            for (WarningInfos w : warningInfos) {
                if (course.contains(w.getStuid())) {
                    if (map.get(w.getStumajor()) != null) map.put(w.getStumajor(), map.get(w.getStumajor()) + 1);
                    else map.put(w.getStumajor(), 1);
                }
            }
            response.getWriter().write(JSONObject.toJSONString(Message.success(map)));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("error in param is null")));
    }

    @RequestMapping("/majors")
    public void showMajors(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String college = "";
        User user = (User)request.getSession().getAttribute("user");
        if(user.getRole()==1){
            college = request.getParameter("college");
        }else college=tm.findTeaByName(user.getName()).getCollege();
        String grade = request.getParameter("grade");
        String term = request.getParameter("terms");
        if(grade!=null&&!"".equals(grade)&&term!=null&&!"".equals(term)) {
            Map<String, Integer> map = new HashMap<>();
            Set<String> course = new HashSet<>();
            List<Student> stuByCollegeAndGrade = stum.findStuByCollegeAndGrade(college, grade);
            for (Student s : stuByCollegeAndGrade) {
                course.add(s.getMajor());
            }
            String courses = String.join(",", course);
            List<WarningInfos> warningInfos = wim.selectbyTerm(term);
            for (WarningInfos w : warningInfos) {
                if (stum.selectStuById(w.getStuid()).getGrade().equals(grade) && courses.contains(w.getStumajor())) {
                    if (w.getWarningtype() == 1) {
                        if (map.get("轻度") != null) map.put("轻度", map.get("轻度") + 1);
                        else map.put("轻度", 1);
                    } else if (w.getWarningtype() == 2) {
                        if (map.get("中度") != null) map.put("中度", map.get("中度") + 1);
                        else map.put("中度", 1);
                    } else {
                        if (map.get("重度") != null) map.put("重度", map.get("重度") + 1);
                        else map.put("重度", 1);
                    }
                }
            }
            response.getWriter().write(JSONObject.toJSONString(Message.success(map)));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("error in param is null")));
    }

    @RequestMapping("/college")
    public void showCollege(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        String grade = request.getParameter("grade");
        String term = request.getParameter("term");
        if(grade!=null&&!"".equals(grade)&&term!=null&&!"".equals(term)) {
            Map<String, Integer> map = new HashMap<>();
            Set<String> course = new HashSet<>();
            Set<String> colleges = new HashSet<>();
            if (user.getRole() == 1) {
                colleges = new HashSet<>();
                List<Student> alllStu = stum.findStuByGrade(grade);
                for (Student s : alllStu) {
                    colleges.add(s.getCollege());
                }
            } else colleges.add(tm.findTeaByName(user.getName()).getCollege());
            List<WarningInfos> warningInfos = wim.selectbyTerm(term);
            for (String s : colleges) {
                List<Student> stuByCollegeAndGrade = stum.findStuByCollegeAndGrade(s, grade);
                for (Student ss : stuByCollegeAndGrade) {
                    course.add(ss.getMajor());
                }
                String coursestring = String.join(",", course);
                for (WarningInfos w : warningInfos) {
                    if (stum.selectStuById(w.getStuid()).getGrade().equals(grade) && coursestring.contains(w.getStumajor())) {
                        if (map.get(s) != null) map.put(s, map.get(s) + 1);
                        else map.put(s, 1);
                    }
                }
            }
            response.getWriter().write(JSONObject.toJSONString(Message.success(map)));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("error in param is null")));
    }

    @RequestMapping("/grade")
    public void showGrade(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        String college="";
        if(user.getRole()==1){
            college = request.getParameter("college");
        }else college=tm.findTeaByName(user.getName()).getCollege();
        String term = request.getParameter("term");
        if(term!=null&&!"".equals(term)) {
            Map<String, Integer> map = new HashMap<>();
            Set<String> grade = new HashSet<>();
            List<Student> alllStu = stum.findStuByCollege(college);
            List<WarningInfos> warningInfos = wim.selectbyTerm(term);
            for (Student s : alllStu) {
                grade.add(s.getGrade());
            }
            for (String gra : grade) {
                List<Student> stuByGrade = stum.findStuByCollegeAndGrade(college,gra);
                Set<Integer> stuIds = new HashSet<>(stuByGrade.size());
                for (Student stu : stuByGrade) {
                    stuIds.add(stu.getId());
                }
                for (WarningInfos w : warningInfos) {
                    if (stuIds.contains(w.getStuid())) {
                        if (map.get(gra) != null) map.put(gra, map.get(gra) + 1);
                        else map.put(gra, 1);
                    }
                }
            }
            response.getWriter().write(JSONObject.toJSONString(Message.success(map)));
        }else response.getWriter().write(JSONObject.toJSONString(Message.failure("error in param is null")));
    }

    @RequestMapping("/out")
    public void out(HttpServletRequest request,HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        List<WarnNum> list=new ArrayList<>();
        if(user.getRole()==2){
            response.getWriter().write(JSONObject.toJSONString(Message.failure("You not have power to download")));
        }
        List<Student> alllStu;
        if(user.getRole()==1){
            alllStu = stum.findAlllStu();
        } else {
            String college=tm.findTeaByName(user.getName()).getCollege();
            String  grade=tm.findTeaByName(user.getName()).getGrade();
            alllStu = stum.findStuByCollegeAndGrade(college,grade);
        }
        int num1=0;
        int num2=0;
        int num3=0;
        for(Student s:alllStu){
            List<WarningInfos> selectbyid = wim.selectbyid(s.getId());
            for(WarningInfos warn:selectbyid){
                if(warn.getWarningtype()==1){
                    num1=num1+1;
                }else if(warn.getWarningtype()==2){
                    num2=num2+1;
                }else num3=num3+1;
            }
            WarnNum warnNum=new WarnNum(s.getId(),s.getName(),s.getGrade(),s.getCollege(),s.getMajor(),num1,num2,num3,num1+num2+num3);
            list.add(warnNum);
             num1=0;
             num2=0;
             num3=0;
        }
        WarnNumUtil util=new WarnNumUtil();
        util.exportExcel(response,list);
    }

    @RequestMapping("/out1")
    public void out1(HttpServletRequest request,HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        List<WarnNum> list=new ArrayList<>();
        if(user.getRole()==2){
            response.getWriter().write(JSONObject.toJSONString(Message.failure("You not have power to download")));
        }
        List<Student> alllStu;
        if (user.getRole() == 1) {
            alllStu = stum.findAlllStu();
        } else {
            String college=tm.findTeaByName(user.getName()).getCollege();
           alllStu = stum.findStuByCollege(college);

        }
        int num1=0;
        int num2=0;
        int num3=0;
        for(Student s:alllStu){
            List<WarningInfos> selectbyid = wim.selectbyid(s.getId());
            for(WarningInfos warn:selectbyid){
                if(warn.getWarningtype()==1){
                    num1=num1+1;
                }else if(warn.getWarningtype()==2){
                    num2=num2+1;
                }else num3=num3+1;
            }
            WarnNum warnNum=new WarnNum(s.getId(),s.getName(),s.getGrade(),s.getCollege(),s.getMajor(),num1,num2,num3,num1+num2+num3);
            list.add(warnNum);
            num1=0;
            num2=0;
            num3=0;
        }
        WarnNumUtil util=new WarnNumUtil();
        util.exportExcel(response,list);
    }

}
